﻿using System;
using System.Collections.Generic;
using System.Linq;
using UMC.Data.Entities;
using UMC.Web;

namespace UMC.ITME.Activities
{

    [UMC.Web.Apiumc("ITME", "Customer", Auth = WebAuthType.All, Desc = "运维客服")]
    class SiteCustomerActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var sites = DataFactory.Instance().Site().Where(r => r.Flag != -1).Where(r => (r.IsModule ?? false) == false)
            .OrderBy((arg) => arg.Seq ?? 1000).ToList();
            sites.Add(new Entities.Site { SiteKey = 0, Caption = "管理员团队" });

            var uTitle = new UITitle("运维客服");
            var ui = UISection.Create(uTitle);
            var IsHav = false;

            foreach (var site in sites)
            {
                User[] users = UMC.Data.DataFactory.Instance().Users(site.SiteKey.Value, UMC.Security.Membership.AdminRole);
                if (users.Length > 0)
                {
                    var ls = users.Select(r => r.Id.Value).ToArray();

                    var ut = Data.HotCache.Cache<UserToRole>().Find(new UserToRole { Site = 0, Rolename = UMC.Security.Membership.UserRole }, "user_id", ls);
                    if (ut.Length > 0)
                    {
                        IsHav = true;
                        var ui2 = ui.NewSection();
                        ui2.Header.Put("text", site.Caption);
                        foreach (var u in users)
                        {
                            if (ut.Any(r => r.user_id == u.Id))
                            {
                                ui2.AddCell('\uf2c0', u.Alias, "", new UIClick("Account", "Staff", u.Username));
                            }

                        }
                    }
                }
            }

            if (IsHav == false)
            {
                ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有应用客服").Put("icon", "\uEA05"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),
                            new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 

            }
            ui.SendTo(this.Context, $"{request.Model}.{request.Command}");
        }
    }
}